import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);


const store=new Vuex.Store({
    state:{
        currentadmin:null,
        msglogin:false,
        uname:"",
        upwd:"",
        uimg:"",
        uhidd:true,
        login:false,
        currentuser:"",
        numshow:"",
        kong:"",
        goodsobj:[],
        userinfo:{
        },
        num:"",
    },
    mutations:{
        adminstatus(state,payload)
        {
            console.log(payload);
            if(payload)
            {
                state.currentadmin=payload;
                state.msglogin=true
            }
            else if(payload==null)
            {
                sessionStorage.setItem("adminname",null);
                // sessionStorage.setItem("adminToken","");
                state.currentadmin=null;
                state.msglogin=false;
            }
        },
        userstatus(state,payload)
        {
            
            if(payload)
            {
                state.currentuser=payload.u_name;
                state.login=true;
                // state.uimg=payload.u_img;
            }
            else if(payload==null)
            {
                sessionStorage.setItem("username",null);
                // sessionStorage.setItem("userimg",null);
                // sessionStorage.setItem("adminToken","");
                state.currentuser=null;
                state.login=false;
            }
            // console.log(state);
        },
        setCookie(state,payload)
        {
            console.log("mutation",payload);
            var exdate = new Date(); //获取时间
                exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * (payload.u_time)); //保存的天数
                //字符串拼接cookie
                window.document.cookie = "userName" + "=" + payload.u_name + ";path=/;expires=" + exdate.toGMTString();
                window.document.cookie = "userPwd" + "=" + payload.u_pwd + ";path=/;expires=" + exdate.toGMTString();
                // window.document.cookie = "userImg" + "=" + payload.u_img + ";path=/;expires=" + exdate.toGMTString();
        },
        getCookie(state,payload)
        {
            // console.log("state",state);
                  if (document.cookie.length > 0) {
                    var arr = document.cookie.split('; '); //这里显示的格式需要切割一下自己可输出看下
                    for (var i = 0; i < arr.length; i++) {
                        var arr2 = arr[i].split('='); //再次切割
                        //判断查找相对应的值
                        if (arr2[0] == 'userName') {
                            state.uname = arr2[1]; //保存到保存数据的地方
                        } else if (arr2[0] == 'userPwd') {
                            state.upwd= arr2[1];
                        }
                    }
                }
                else{
                    state.uname="";
                    state.upwd="";
                }
                  if(state.uname)
                        {
                            state.uhidd=true
                        }
                        else{
                            state.uhidd=false
                        }      
        },
        huoqus(state,payload)
        {
            // console.log(payload);
            state.uname=payload.u_name;
            // state.uimg=payload.u_img;
            console.log(state.uname,state.uimg);
        },
        cartnum(state,_this)
        {
            // console.log(_this);
            if(!sessionStorage.getItem("userid"))
            {
                state.numshow=false
            }
           else{
               state.numshow=true;
            _this.$axios.post("http://127.0.0.1:8081/client/cartnum",{ u_id:sessionStorage.getItem("userid")}).then(res=>
            {
                state.num=res.data.num;
            })
           }
        },
        carts(state,payload)
        {
            state.num=state.num+payload;
        },
        cartdeta(state,payload)
        {
            state.goodsobj=[];
            payload.$axios.post("http://127.0.0.1:8081/client/cart",
            { u_id:sessionStorage.getItem("userid")}).then(res=>
             {
               console.log("res",res.data.result);
               if(res.data.result.length==0)
               {
                // console.log(res,res.data.result.length);
                 state.kong=true;
                //  console.log("this.kong",state.kong)
               }
               else{
                 state.kong=false;
                    for(let i=0;i<res.data.result.length;i++)
               {
                 
                 res.data.result[i].checked=false;
                    // console.log(res.data)
                    // if(res.data.result[i].c_style.length==0)
                    // {
                    //     res.data.result[i].c_style=""
                    // }
                 res.data.result[i].c_style=JSON.parse(res.data.result[i].c_style);

                //  console.log(state.goodsobj.length);
                 if(state.goodsobj.length==0)
                 {
                      console.log("this.goodsobj.length==0",res.data.result[i]);
                   state.goodsobj.push({name:res.data.result[i].c_company});
                   
                   state.goodsobj[0].list=[];
                   state.goodsobj[0].list.push(res.data.result[i]);
                   state.goodsobj[0].checked=false;
                 }
                else{
                    console.log("state",state.goodsobj)
                   let flag=true;
                   for(let j=0;j<state.goodsobj.length;j++)
                 {

                   // console.log(state.goodsobj[j].name==res.data.result[i].c_company);
                 if(state.goodsobj[j].name==res.data.result[i].c_company)
                 {
                   // console.log(j,state.goodsobj[j].name)
                   state.goodsobj[j].list.push(res.data.result[i]);
                   flag=true;
                   break;
                 }
                 else{
                    flag=false;
                //    state.goodsobj.push({name:res.data.result[i].c_company});
                //    state.goodsobj[j+1].list=[res.data.result[i]];
                //    state.goodsobj[j+1].checked=false;
                //    break;
                 }
                 // console.log(state.goodsobj[j].name) 
                 }
                 if(!flag)
                 {
                    state.goodsobj.push({name:res.data.result[i].c_company});
                    state.goodsobj[state.goodsobj.length-1].list=[res.data.result[i]];
                    state.goodsobj[state.goodsobj.length-1].checked=false;
                 }
                }
               }
               }
            
               console.log("goods",state.goodsobj);
               // console.log(this.goodsobj)
             }) 
        }
    },
    actions:{
        setadmin({commit},payload)
        {
            // console.log("action",payload);
            commit("adminstatus",payload)
        },
        set({commit},payload)
    {
        console.log("active",payload);
        commit("setCookie",payload);
    },
    get({commit},payload)
    {
        commit("getCookie",payload);
    },
    // huoqu({commit},payload)
    // {
    //     commit("huoqus",payload);
    // },
    setuser({commit},payload)
        {
            // console.log("action",payload);
            commit("userstatus",payload)
        },
    cartnum({commit},_this)
    {
        commit("cartnum",_this);
    },
    carts({commit},payload)
    {
        commit("carts",payload);
    },
    cartdeta({commit},payload)
    {
        console.log("payload",payload)
        commit("cartdeta",payload)
    }

    },

    
    getters: {
        
    }
})
export default store;